package com.example.core.tag;

import cn.hutool.core.bean.BeanUtil;
import com.example.core.form.BaseForm;
import com.example.sys.module.mode.SysColumnInfo;
import com.example.sys.module.mode.SysModuleInfo;
import com.example.sys.module.service.SysModuleService;
import com.example.utils.SpringUtils;
import com.example.utils.StringUtils;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;

@Data
public class XInput extends BodyTagSupport {

    public SysModuleService sysModuleService;

    private String titleWidth = "15%";

    private String propertyName;

    private String colspan;

    private String width = "35%";

    private Boolean required = false;

    private Boolean readonly = false;

    private String value;

    private String placeholder;

    public SysModuleInfo getSysModuleInfo(PageContext pageContext) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        String entityName = (String) httpServletRequest.getAttribute("entityName");
        if (!StringUtils.isEmpty(entityName)) {
            return this.getSysModuleService().getSysModuleInfoBySimpleName(entityName);
        }
        return null;
    }

    public Object getDefaultValue(PageContext pageContext) {
        HttpServletRequest httpServletRequest = (HttpServletRequest) pageContext.getRequest();
        String formName = (String) httpServletRequest.getAttribute("formName");
        if (!StringUtils.isEmpty(formName)) {
            BaseForm baseForm = (BaseForm) httpServletRequest.getAttribute(formName);
            return BeanUtil.getFieldValue(baseForm, this.propertyName);
        }
        return null;
    }

    public SysModuleService getSysModuleService() {
        if (sysModuleService == null) {
            sysModuleService = (SysModuleService) SpringUtils.getBean(SysModuleService.class);
        }
        return sysModuleService;
    }

    @Override
    public int doStartTag() throws JspException {
        JspWriter out = pageContext.getOut();
        SysModuleInfo sysModuleInfo = this.getSysModuleInfo(pageContext);
        this.getDefaultValue(pageContext);
        try {
            SysColumnInfo sysColumnInfo = sysModuleInfo.getSysColumnInfo(this.propertyName);
            if (!StringUtils.isEmpty(sysColumnInfo.getComment())) {
                out.print("<td class=\"tb-title\" style=\"width: " + titleWidth + "\"> " + sysColumnInfo.getComment() + "</td>");
            } else {
                out.print("<td class=\"tb-title\" style=\"width: " + titleWidth + "\"></td>");
            }
            if (StringUtils.isEmpty(this.colspan)) {
                out.print("<td style=\"width: " + this.width + "\">");
            } else {
                out.print(" <td colspan=\"" + this.colspan + "\">");
            }
            if (this.required) {
                out.print(" <input type=\"text\" name=\"" + this.propertyName + "\" lay-verify=\"required\"\n" + " class=\"layui-input\"");
            } else {
                out.print("<input type=\"text\" name=\"" + this.propertyName + "\" class=\"layui-input\"");
            }
            Object defaultValue = this.getDefaultValue(pageContext);
            if (defaultValue != null) {
                out.print(" value=\"" + defaultValue + "\"");
            }
            if (this.readonly) {
                out.print(" readonly /> ");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doEndTag() throws JspException {
        JspWriter out = pageContext.getOut();
        try {
            out.print("</td>");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return EVAL_PAGE;
    }
}
